********************************************************************************
* Purpose: Sample selection table 
*********************************************************************************

do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

*********************************************************************************
* Table 1: sample selection (top panel; only admin and NOVAX data)
*********************************************************************************

* 1: All births between 2012-2017
use "$work\mfr_sample",clear
egen allbirths=count(mfr_yob)

merge m:1 mfr_muni using "$work\novaxmuni", keep(3) nogen // 231,339 pnrs merged from most general novax data

*1: novax munics with good coverage years in novax (contacts file)
keep if mfr_yob>=treated
drop treated

* drop multiple births here. not in same groups
drop if mfr_multiplebirth==1 //219,239
egen allnovax = count(mfr_yob) 

* 2: Only munics that use novax to register both groups and screenings: 
merge m:1 mfr_yob mfr_muni using "$work\munics_grupper", keep(3) nogen

merge m:1 mfr_yob mfr_muni using "$work\munics_tested", keep(3) keepusing(mfr_yob mfr_muni)
egen novaxmunigroups = count(mfr_yob) 

* 3: individuals in a group:  
merge 1:1 pnr using "$work\grupper_wide" , keep(3) nogen

egen group = group(GRUPPEID1)
egen ingroup = count(mfr_yob)

* 4: Drop unrealistically small groups (Groups with less than 3): 76,747
egen g_members = count(mfr_yob), by(group)
drop if g_members < 3
egen ingroup1 = count(mfr_yob)

* 5: In group were all individuals are tested:
merge 1:1 pnr using "$work\efterfodsel_mor.dta", keep(1 3) nogen
gen tested = 0
replace tested = 1 if score_e1 != .
rangestat tested, int(group 0 0) excludeself
drop if tested == 0
drop if tested_mean != 1
egen finalcut = count(mfr_yob)

eststo clear
eststo: estpost summarize allbirths allnovax novaxmunigroups ingroup finalcut
est store a

esttab a using "$out\sampleselection.tex", fragment nonum mlabels(none) label cells("mean(fmt(%13.2fc))") stats(N, fmt(%13.0fc)) collabels(\multicolumn{1}{c}{Mean}) nodepvar alignment(c c) nolegend nonote replace
